package com.yantong.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.yantong.constant.ActivityType;
import com.yantong.mapper.ActivityMapper;
import com.yantong.mapper.ActivityProductMapper;
import com.yantong.mapper.LocalLifeMapper;
import com.yantong.pojo.*;
import com.yantong.service.LocalLifeService;
import com.yantong.service.MemberService;
import com.yantong.utils.DateUtil;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LocalLifeServiceImpl implements LocalLifeService {
	@Autowired
	private LocalLifeMapper localLifeMapper;
	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private ActivityProductMapper activityProductMapper;
	@Autowired
	private MemberService memberService;


	@Override
	public List<Activity> getGiftPackagesByFreechargeId(Long storeFreechargeId) {
		List<Activity> otherActivitList = activityMapper.selectListByStoreFreechargeId(storeFreechargeId);
		if (ValidateUtil.isNotEmptyCollection(otherActivitList)) {
			for (Activity activity : otherActivitList) {
				ActivityProductExample example = new ActivityProductExample();
				example.createCriteria().andActivityIdEqualTo(activity.getId());
				List<ActivityProduct> list = activityProductMapper.selectActivityProductByActivityId(activity.getId());
				List<String> listIds = new ArrayList<>();
				List<String> listProductIds = new ArrayList<>();
				if(ValidateUtil.isNotEmptyCollection(list)){
					for(ActivityProduct activityProduct:list){
						if(ValidateUtil.isNotEmptyString(activityProduct.getCouponIds())){
							String [] str=activityProduct.getCouponIds().split(",");
							Collections.addAll(listIds,str);
						}
						if(ValidateUtil.isNotEmptyString(activityProduct.getFreechargeId())){
							String [] strProduct=activityProduct.getFreechargeId().split(",");
							Collections.addAll(listProductIds,strProduct);
						}

					}
				}
				//获取优惠券的数量
				activity.setActivityProductCount((int) activityProductMapper.countByExample(example));
				activity.setJoinNum(listIds.size());
			}
		}
		return otherActivitList;
	}

	@Override
	public Map<String, Object> getGiftPackages(Integer plateType,Long shopId, String longitude, String latitude,
											   Integer hotFlag, Long centreId, int offset, int limit,String sidx,String order) {
		Map<String, Object> resultMap = new HashMap<>();

		Map<String, Object> queryParam = new HashMap<>();
		queryParam.put("teamId", shopId);
		queryParam.put("centreId", centreId);
		queryParam.put("limitOne", 1);
		queryParam.put("type", ActivityType.FREECHARGE.getType());
		queryParam.put("nowDate", DateUtil.convertDateToStr(new Date(), DateUtil.yyyy_MM_dd_HH_mm_ss));
		//获取单店的大礼包，只有第一页才会去查
		if (offset == 0) {
			Activity selfActivity = activityMapper.getActivityByShopId(queryParam);
			if (null != selfActivity) {
				ActivityProductExample example = new ActivityProductExample();
				example.createCriteria().andActivityIdEqualTo(selfActivity.getId());
				selfActivity.setActivityProductCount((int) activityProductMapper.countByExample(example));
			}
			resultMap.put("selfPackages", selfActivity);
		}

		//获取其他门店的分页大礼包
		queryParam.put("teamId", null);
		//queryParam.put("excludeShopId", shopId);
		queryParam.put("longitude", longitude);
		queryParam.put("latitude", latitude);
		queryParam.put("centreId", centreId);
		if(plateType == null){
			queryParam.put("plateType",0);
		}else{
			queryParam.put("plateType",plateType);
		}
		queryParam.put("limitOne", 0);
		queryParam.put("status", 1);

		if (hotFlag != null && hotFlag == 1) {
			Member member = memberService.getByKey(shopId);
			if(member != null){
				queryParam.put("headOfficeId", member.getId());
			}
		}

		//排序
		List<String> orderList = new ArrayList<>();
		orderList.add("activity_price");
		orderList.add("sold");
		if ( orderList.contains(sidx) ) {
			if (StringUtil.isNotEmpty(order)) {
				queryParam.put("orderByClause",sidx+" desc");
			} else {
				queryParam.put("orderByClause",sidx);
			}
		}else{
			queryParam.put("column", "distance");
		}

		PageUtils<Activity> otherPackagePageUtils = new PageUtils<>();
		otherPackagePageUtils.setTotal(activityMapper.queryTotal(queryParam));
		List<Activity> otherActivitList = activityMapper.pageListBycondition(queryParam, new RowBounds(offset, limit));
		if (ValidateUtil.isNotEmptyCollection(otherActivitList)) {
			for (Activity activity : otherActivitList) {
				ActivityProductExample example = new ActivityProductExample();
				example.createCriteria().andActivityIdEqualTo(activity.getId());
				List<ActivityProduct> list = activityProductMapper.selectActivityProductByActivityId(activity.getId());
				List<String> listIds = new ArrayList<>();
				List<String> listProductIds = new ArrayList<>();
				if(ValidateUtil.isNotEmptyCollection(list)){
					for(ActivityProduct activityProduct:list){
						if(ValidateUtil.isNotEmptyString(activityProduct.getCouponIds())){
							String [] str=activityProduct.getCouponIds().split(",");
							Collections.addAll(listIds,str);
						}
						if(ValidateUtil.isNotEmptyString(activityProduct.getFreechargeId())){
							String [] strProduct=activityProduct.getFreechargeId().split(",");
							Collections.addAll(listProductIds,strProduct);
						}

					}
				}
				//获取优惠券的数量
				//activity.setJoinNum(storeFreechargeMapper.getCounpCount(activity.getId()));
				activity.setActivityProductCount((int) activityProductMapper.countByExample(example));
				activity.setJoinNum(listIds.size());
				//activity.setActivityProductCount(listProductIds.size());
			}
		}
		otherPackagePageUtils.setRows(otherActivitList);
		resultMap.put("otherPackages", otherPackagePageUtils);

		return resultMap;
	}

	@Override
	public PageUtils<Statistics> statisticsList(Long teamId) {
		PageUtils<Statistics> pageUtils = new PageUtils<>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("teamId", teamId);
		int total = localLifeMapper.countByParams(params);
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(localLifeMapper.listByParams(params, new RowBounds(0, 1000)));
		}
		return pageUtils;
	}
}
